package be.bonamis.timesheet.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import be.bonamis.timesheet.dto.PrestationDTO;
import be.bonamis.timesheet.model.Prestation;
import be.bonamis.timesheet.repository.PersonRepository;
import be.bonamis.timesheet.repository.PrestationRepository;
import be.bonamis.timesheet.repository.PrestationTypeRepository;
import be.bonamis.timesheet.repository.ProjectRepository;
import be.bonamis.timesheet.service.PrestationService;

/**
 * This implementation of the ProjectService interface communicates with the
 * database by using a Spring Data JPA repository.
 * 
 * @author Florent Bonamis
 */
@Service
public class RepositoryPrestationService implements PrestationService{

	@Resource
	private PrestationRepository prestationRepository;
	
	@Resource
	private PersonRepository personRepository;

	@Resource
	private PrestationTypeRepository prestationTypeRepository;
	
	@Resource
	private ProjectRepository projectRepository;
	
	

	private static final Logger LOGGER = LoggerFactory
			.getLogger(RepositoryProjectService.class);

	@Transactional
	@Override
	public Prestation create(PrestationDTO created) {
		LOGGER.debug("Creating a new Prestation with information: " + created);
		
		Prestation prestation = new Prestation();
		DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy");
		DateTime dateTime = format.parseDateTime(created.getPrestationDate());
		prestation.setDay(new LocalDateTime(dateTime));
		prestation.setPerson(personRepository.findOne((long) created.getPersonId()));
		prestation.setProject(projectRepository.findOne((long) created.getProjectId()));
		prestation.setTime(created.getDuration());
		prestation.setType(prestationTypeRepository.findOne((long) created.getTypeId()));

		return prestationRepository.save(prestation);
	}

//	@Transactional(rollbackFor = ProjectNotFoundException.class)
	@Override
	public Prestation delete(Long projectId) {
//		LOGGER.debug("Deleting project with id: " + projectId);
//
//		Project deleted = projectRepository.findOne(projectId);
//
//		if (deleted == null) {
//			LOGGER.debug("No project found with id: " + projectId);
//			throw new ProjectNotFoundException();
//		}
//
//		projectRepository.delete(deleted);
//		return deleted;
		return null;
	}

	@Transactional(readOnly = true)
	@Override
	public List<Prestation> findAll() {
		LOGGER.debug("Finding all prestations ");
		return prestationRepository.findAll();
	}

	@Transactional(readOnly = true)
	@Override
	public Prestation findById(Long id) {
		LOGGER.debug("Finding project by id: " + id);
		return prestationRepository.findOne(id);
	}

	//@Transactional(rollbackFor = PersonNotFoundException.class)
	@Override
	public Prestation update(Prestation updated)  {
//		LOGGER.debug("Updating person with information: " + updated);
//
//		Project project = projectRepository.findOne(updated.getId());
//
//		if (project == null) {
//			LOGGER.debug("No person found with id: " + updated.getId());
//			throw new ProjectNotFoundException();
//		}
//
//		project.update(updated.getName());
//
//		return project;
		return null;
	}

	@Transactional(readOnly = true)
	@Override
	public List<Prestation> findByProject(long projectId) {		
		return prestationRepository.find(projectId);
	}

}
